$(function (){
    //查询全部父类和子类
    $.get(
        "../HtmlProductCategory",
        function (msg){
            let parents=msg.data;
            for (let i = 0; i < parents.length; i++) {
                let parrent=$(`
                           <li class="bg-light mb-2 p-2 rounded">
                                    <a href="#category-${parents[i].id}" data-toggle="collapse">
                                        <i class="glyphicon-th-large"></i>
                                        <span>${parents[i].name}</span>
                                        <i class="dropdown-toggle"></i>
                                    </a>
                                    <div class="float-right">
                                        <a href="#modal-edit-primary-category" class="btn btn-sm btn-outline-info py-0 parent" data-toggle="modal" data-id="${parents[i].id}">修改</a>
                                        <a href="#" class="btn btn-sm btn-outline-danger py-0 pdelete"  data-id="${parents[i].id}" data-pid="${parents[i].parentid}">移除</a>
                                    </div>
                                    
                                    <div id="category-${parents[i].id}" class="collapse active">
                                        <ul class="list-unstyled ml-4 mt-2"></ul>
                                    </div>
                           </li>`);
                //初始化父亲数据
               var parentName=$(`
               <option value="${parents[i].id}">${parents[i].name}</option>
               `);
               $("#parName").append(parentName);
                //查询全部子类
               let sons= parents[i].sons;
                for (let j = 0; j < sons.length; j++) {
                    let son=$(`<li>
                                   <i class="glyphicon-th"></i>
                                   <label>${sons[j].name}</label>
                                   <div class="float-right">
                                       <a href="#modal-edit-category" class="btn btn-sm btn-outline-info py-0 sons" data-toggle="modal" data-id="${sons[j].id}">修改</a>
                                       <a href="#" class="btn btn-sm btn-outline-danger py-0 sons" data-id="${sons[j].id}">移除</a>
                                   </div>
                               </li>`);
                    //追加到儿子里面

                    parrent.find(".list-unstyled").append(son)
                }

                $("#parent").append(parrent)
            }

        },
        "json"
    )
    //查询全部结束

   //添加类别开始
        $("#sava-btn").click(function (){
            //判断选中的是父还是子
           if ($("[name=ctype]:checked").val()=="hide"){
             var name=$("[name=name]").val();
               var parentId=0;

           }else {
               var name=$("[name=name]").val();
               var parentId=$("#parName").val();

           }
            //调用添加的方法
            $.post(
                "../HtmlCateSava",
                {name:name,parentId:parentId},
                function (msg){
                    if (msg.data==1){
                        alert("添加成功");
                        location.href="product-category.html";
                    }else {
                        alert("添加失败");
                    }
                },
            "json"
            )
        });
    //添加商品类别结束

    //单个查询父类
    $("#parent").on("click","li  > div  > .parent",function (){
        let patId = $(this).data("id");

        $.get(
            "../HtmlCategoryById",
            {patId:patId},
            function (msg){
              let patName= msg.data
                //赋值
                $("#patName").val(patName.name);
                $("[name=id]").val(patName.id);
            },
            "json"
        )

        //修改父类
       $("#update-parent-btn").click(function (){
           $.get(
               "../HtmlCategoryParentUpdate",
               $("#form-edit-primary-category").serialize(),
               function (msg){
                   if (msg.data==1){
                       alert("修改成功");
                       location.reload();
                   }else {
                       alert("修改失败");
                   }
               },
               "json"
           )
       })
    });

  /*  //单个查询子类
    $("#parent").on("click","li  > div > ul > li > div > .sons",function (){
        let patId = $(this).data("id");
        $.get(
            "../CategoryParentById",
            {patId:patId},
            function (msg){
                let patName= msg.data
                //赋值
                $("#parentName").text(patName.name);
                $("[name=id]").val(patName.id);
            },
            "json"
        )

      /!*  //修改父类
        $("#update-parent-btn").click(function (){
            $.get(
                "../HtmlCategoryParentUpdate",
                $("#form-edit-primary-category").serialize(),
                function (msg){
                    if (msg.data==1){
                        alert("修改成功");
                        location.reload();
                    }else {
                        alert("修改失败");
                    }
                },
                "json"
            )
        })*!/
    });*/

    //删除
    $("#parent").on("click","li  > div  > .pdelete",function (){
        let id = $(this).data("id");
        let pid=$(this).data("pid");
       if (confirm("确定要删除吗")){
           $.get(
               "../HtmldeleteById",
               {id:id,pid:pid},
               function (msg){
                   console.log(msg);
                   if (msg.data==1){
                       alert("删除成功");
                       //刷新页面
                       location.reload();
                   }else {
                       alert("该商品有子类，删除失败");
                   }
               },
               'json'
           )
       }
    });
    $("#parent").on("click","li  > div > ul > li > div > .sons",function (){
        let id=$(this).data("id");
        if (confirm("确定要删除吗")){
            $.get(
                "../HtmlSonDelete",
                {id:id},
                function (msg){
                    console.log(msg);
                    if (msg.data==1){
                        alert("删除成功");
                        //刷新页面
                        location.reload();
                    }else {
                        alert("删除失败");
                    }
                },
                'json'
            )
        }
    });
});